<?php
    class UserMapper {
        private
            $responses = array();
        const
            MYSQL_QUERY_ERROR = 'Query error to data base.',
            EMAIL_EMPTY = 'E-mail empty.',
            EMAIL_EXISTS = 'E-mail exists.',
            EMAIL_INSERTED = 'E-mail inserted.',
            USER_INSERTED = 'User inserted.',
            LOG_IN_OK = 'Log in ok.',
            LOG_IN_NO = 'Log in error.',
            LOG_OUT_OK = 'Log out ok.';
        function __construct(){
            $varsConnect = array(
                'host' => 'localhost',
                'login' => 'root',
                'password' => '',
                'name' => 'embria3'
            );
            mysql_connect($varsConnect['host'], $varsConnect['login'], $varsConnect['password']);
            mysql_select_db($varsConnect['name']);
            mysql_query('set character_set_client="utf-8"');
            mysql_query('set character_set_results="utf-8"');
        }
        function getResponses(){
            return $this->responses;
        }
        function setUser($user){
            session_start();
            $_SESSION['user']['id'] = $user->id;
            $_SESSION['user']['email'] = $user->email;
        }
        function getCurrentUser(){
            session_start();
            if (isset($_SESSION['user'])) {
                return $_SESSION['user'];
            } else {
                $user = new UserModel(
                    array(
                        'rights' => 'anon'
                    )
                );
                $_SESSION['user'] = $user;
                return $user;
            }
        }
        function logIn($user){
            $query = mysql_query("SELECT * FROM users WHERE
                email='" . $user->email . "' AND
                password= '" . $user->password . "'
            ");
            $row = mysql_fetch_assoc($query);
            if ($row){
                $this->responses[] = self::LOG_IN_OK;
                $user->id = $row['id'];
                $user->rights = $row['rights'];
                session_start();
                $_SESSION['user'] = $user;
            } else {
                $this->responses[] = self::LOG_IN_NO;
            }
        }
        function logOut(){
            session_start();
            unset($_SESSION['user']);
            $user = new UserModel(
                array(
                    'rights' => 'anon'
                )
            );
            $_SESSION['user'] = $user;
            $this->responses[] = self::LOG_OUT_OK;
        }
        function insert($user){
            $query = mysql_query("SELECT * FROM users WHERE
                email='" . $user->email . "'
            ");
            $row = mysql_fetch_assoc($query);
            if (empty($row)){
                $query = mysql_query("INSERT INTO users VALUES (
                    NULL,
                    '" . $user->email . "',
                    '" . $user->password . "',
                    'client'
                )");
                if ($query){
                    $this->responses[] = self::USER_INSERTED;
                    session_start();
                    $_SESSION['user'] = $user;
                } else {
                    $this->responses[] = self::MYSQL_QUERY_ERROR;
                }
            } else {
                $this->responses[] = self::EMAIL_EXISTS;
            }
        }
    }
?>
            